using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Microsoft.CommerceServer.SecureSite
{
	public class Completed : Microsoft.CommerceServer.SecureSite.Wizard
	{
		private bool applyingSettings = false;
		private System.Windows.Forms.ProgressBar InstallProgress;
		private System.Windows.Forms.TextBox txtStatus;
		private System.ComponentModel.IContainer components = null;

		public Completed()
		{
			// This call is required by the Windows Form Designer.
			InitializeComponent();
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			// 
			// btnNext
			// 
			this.btnNext.Name = "btnNext";
			// 
			// btnPrevious
			// 
			this.btnPrevious.Name = "btnPrevious";
			// 
			// Completed
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(552, 374);
			this.Name = "Completed";
			this.Closing += new System.ComponentModel.CancelEventHandler(this.Completed_Closing);
			this.Load += new System.EventHandler(this.Completed_Load);

		}
		#endregion

		private void Completed_Load(object sender, System.EventArgs e)
		{
			this.InstallProgress = new System.Windows.Forms.ProgressBar();
			this.txtStatus = new System.Windows.Forms.TextBox();
			this.SuspendLayout();
			// 
			// btnNext
			// 
			this.btnNext.Name = "btnNext";
			// 
			// btnPrevious
			// 
			this.btnPrevious.Name = "btnPrevious";
			// 
			// InstallProgress
			// 
			this.InstallProgress.Location = new System.Drawing.Point(32, 96);
			this.InstallProgress.Name = "InstallProgress";
			this.InstallProgress.Size = new System.Drawing.Size(488, 23);
			this.InstallProgress.TabIndex = 4;
			// 
			// txtStatus
			// 
			this.txtStatus.BackColor = System.Drawing.Color.White;
			this.txtStatus.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.txtStatus.Location = new System.Drawing.Point(32, 136);
			this.txtStatus.Multiline = true;
			this.txtStatus.ScrollBars = System.Windows.Forms.ScrollBars.Both;
			this.txtStatus.Name = "txtStatus";
			this.txtStatus.Size = new System.Drawing.Size(488, 176);
			this.txtStatus.TabIndex = 4;
			// 
			// Completed
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(552, 374);
			this.Controls.Add(this.txtStatus);
			this.Controls.Add(this.InstallProgress);
			this.Name = "Completed";
			this.Controls.SetChildIndex(this.InstallProgress, 0);
			this.Controls.SetChildIndex(this.txtStatus, 0);
			this.ResumeLayout(false);

			CommerceSecurityInfo.Log("FORM: Complted form loaded");
			base.Title = "Applying Security Settings...";
			base.btnNext.Text = "Exit";
			base.btnPrevious.Visible = false;
			
			base.btnNext.Enabled = false;
			applyingSettings = true;
			
			this.Show();
			this.Refresh();
			applySecuritySettings();

			applyingSettings = false;
			base.btnNext.Enabled = true;

		}

		protected override void btnNext_Click(object sender, System.EventArgs e)
		{
			CommerceSecurityInfo.CloseAll();
		}

		private void applySecuritySettings()
		{
			this.InstallProgress.Maximum = 8;
			this.InstallProgress.Step = 1;
			CommerceSecurityInfo.Status = this.txtStatus;

			CommerceSecurityInfo.ApplyUser();
			this.InstallProgress.PerformStep();

			if (false == CommerceSecurityInfo.LogonWithCredentials(CommerceSecurityInfo.Username, CommerceSecurityInfo.Password, CommerceSecurityInfo.Domain))
			{
				int ret = Marshal.GetLastWin32Error();
				string error = "LogonUser failed with error code: "+ret+ "Error Message: "+CommerceSecurityInfo.GetErrorMessage(ret);
				CommerceSecurityInfo.Log(error);
				CommerceSecurityInfo.SetStatus("==================================");
				CommerceSecurityInfo.SetStatus(error);
				CommerceSecurityInfo.SetStatus("==================================");
			}
			else
			{

				CommerceSecurityInfo.ApplyIISApplicatonPool();
				this.InstallProgress.PerformStep();

				CommerceSecurityInfo.ApplyCatalogWebService();
				this.InstallProgress.PerformStep();

				CommerceSecurityInfo.ApplyMarketingWebService();
				this.InstallProgress.PerformStep();

                CommerceSecurityInfo.ApplyOrdersWebService();
                this.InstallProgress.PerformStep();

                CommerceSecurityInfo.ApplyProfilesWebService();
                this.InstallProgress.PerformStep();

				CommerceSecurityInfo.ApplyUserToDatabaseRoles();
				this.InstallProgress.PerformStep();

				CommerceSecurityInfo.ApplyUserToCOMPlus();
				this.InstallProgress.PerformStep();

				CommerceSecurityInfo.SetStatus("Done");
			}

			base.Title = "Completed Securing Commerce Server";
		}

		private void Completed_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			if(applyingSettings)
			{
				if(DialogResult.Cancel==MessageBox.Show(this, "Are you sure you want to close the form? The security setting will be in an inconsistent state.", "Close Application", MessageBoxButtons.OKCancel, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button2))
				{
					e.Cancel = true;
				}
			}
		}
	}
}

